<template>
    <div class="business-wrap">
        <div class="search-area">
            <div class="search-box">
                <div @click="handleToSearch" class="search-input">
                    <img class="search-icon" :src="searchIcon" alt="">
                    <div class="search-text">输入商品名称或类目</div>
                    <!-- <div class="search-btn">搜索</div> -->
                </div>
            </div>
        </div>
        <div class="content">
            <div class="sidebar">
                <van-sidebar v-model="activeKey">
                    <van-sidebar-item v-for="item,index in sidebar" :key="index" :title="item.name"  @click="handleSidebarChange(item)"/>
                </van-sidebar>
            </div>
            <div class="cate_list">
                <div class="cate_tab">
                    <div v-for="item, index in tabbar" :key="index" @click="handleTabChange(index)"
                        :class="{ 'cate_active': tabCurrent == index }">{{ item.name }}</div>
                </div>
                <van-list v-model="loadingPage" :finished="finished" :finished-text="list.length ? '没有更多了' : ''"
                    @load="ajaxGoodList" :immediate-check="false">

                    <div v-if="finished && !list.length" class="empty-good">
                        <van-empty description="暂无商品" />
                    </div>

                    <!-- 商品列表 -->
                    <!-- 商品列表 -->
                    <div class="good-ul">
                        <div v-for="item, index in list" :key="index" class="good-ul-li"@click="handleToItem(item.id)">
                            <img :src="item.cover_url" alt="">
                            <div class="good-info">
                                <div class="good-name">{{ item.name }}</div>
                                <div class="good-price">
                                    <div class="price">{{ item.price }}</div>
                                    <!--  v-if="item.price<item.price_original" -->
                                    <div class="original-price">原价: <span>{{ item.price_original }}</span></div>
                                </div>
                            </div>
                        </div>
                    </div>

                </van-list>
            </div>
        </div>

        <footTab :current="1"></footTab>
    </div>
</template>

<script>
import footTab from '@/components/foot-tab';
import axios from 'axios';
import { searchIcon } from '@/tool/static-img';
import { GOOD_LIST ,CATE_LIST} from '@/api/home';
export default {
    components: {
        footTab,
    },
    data() {
        return {
            searchIcon,
            // 分页
            page: 1,
            pageSize: 20,
            list: [],
            loadingPage: false,
            finished: false,
            activeKey: 0,
            tabCurrent: 0,
            tabbar: [
                {
                    id: 0,
                    name: '全部'
                },
                {
                    id: 1,
                    name: '今日特价'
                },
                {
                    id: 2,
                    name: '新品'
                }
            ],
            sidebar:[],
            cate_id:null

        }
    },
    activated() {
        document.title = '分类';
    },
    mounted() {
        this.getHomeData()
    },
    methods: {
        // 去搜索页
        handleToSearch() {
            this.$router.push({
                name: 'search'
            })
        },
        //分类
        getHomeData() {
			this.$http.get(CATE_LIST+0)
				.then(res => {
					const {
						data
					} = res;
					// this.banners = data.banners;
					this.sidebar = data;
                    this.cate_id = this.sidebar[0].id
                    this.ajaxGoodList()
				})
		},
        //头部切换
        handleTabChange(index){
            this.tabCurrent = index;
            this.resetInit()
        },
        //侧边栏切换
        handleSidebarChange(e){
            // console.log(e);
            this.cate_id = e.id
            this.resetInit()
            
        },
        resetInit() { //重置
            this.page = 1;
            this.list = [];
            this.finished = false;
            this.loadingPage = true;
            this.ajaxGoodList()
        },
        // 列表请求
        ajaxGoodList() {
            let params = {};
            params['page_num'] = this.page;
            params['page_size'] = this.pageSize;
            params['cate_id']=this.cate_id
            params['type']=this.tabCurrent
            this.$http.post(GOOD_LIST, params)
                .then(res => {
                    const {
                        list
                    } = res.data;

                    if (list && list.length) {
                        this.page++;
                        this.list = this.list.concat(list);
                        // 加载状态结束
                        this.loadingPage = false;
                        if (list.length < this.pageSize) {
                            this.finished = true;
                        }
                    } else {
                        this.finished = true;
                    }
                })
        },
        //详情
		handleToItem(id) {
			localStorage.setItem('goodmsg', 1)
			this.$router.push({
				name: 'good',
				query: {
					id: id
				}
			})

		},
    }

}

</script>
<style lang="scss" scoped>
.business-wrap {
    min-height: 100vh;
    background-color: #fff;
    padding-bottom: calc(1rem + constant(safe-area-inset-bottom));
    padding-bottom: calc(1rem + env(safe-area-inset-bottom));
    box-sizing: border-box;

    .van-sidebar-item--select::before {
        height: .6rem;
        background: #FF6200;
        border-radius: .04rem;

    }

    .search-area {
        width: 100%;
        // height: 2.72rem;
        padding-top: 0.2rem;
        box-sizing: border-box;
        background-size: cover !important;
        background-color: #fff;
        position: fixed;
        .search-box {
            display: flex;
            align-items: center;
            width: 95%;
            height: 0.72rem;
            margin: auto;

            .search-input {
                position: relative;
                display: flex;
                align-items: center;
                width: 100%;
                height: 0.72rem;
                border-radius: 0.42rem;
                background-color: #F4F4F4;

                .search-icon {
                    width: 0.32rem;
                    height: 0.32rem;
                    margin-left: 0.28rem;
                    margin-right: 0.16rem;
                }

                .search-text {
                    height: 0.4rem;
                    line-height: 0.4rem;
                    font-weight: 400;
                    font-size: 0.28rem;
                    color: #c3c3c3;
                }

                .search-btn {
                    position: absolute;
                    right: 0.06rem;
                    top: 0;
                    bottom: 0;
                    margin: auto;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    width: 1.12rem;
                    height: 0.6rem;
                    margin-left: 0.88rem;
                    font-weight: 400;
                    font-size: 0.28rem;
                    color: #ffffff;
                    border-radius: 0.3rem;
                    background: linear-gradient(270deg, #FD6D17 0%, #FF5E40 100%);
                }
            }

            .kefu-icon {
                width: 0.48rem;
                height: 0.48rem;
                margin-left: 0.32rem;
            }

            .help-icon {
                width: 0.48rem;
                height: 0.48rem;
                margin-left: 0.24rem;
            }
        }
    }

    .content {
        display: flex;
        justify-content: flex-end;
        padding-top: 1rem;
        background: #fff;
        .sidebar {
            // width: 21%;
            height: 100vh;
            // overflow: auto;
            background-color: #f7f8fa;
            margin-top: .2rem;
            position: fixed;
            left: 0;
            // top:1rem ;
            // bottom: 1rem;
            text-align: center;
            

        }
        .van-sidebar-item{
            padding: .3rem .24rem;
        }
        .sidebar::-webkit-scrollbar {
	        display: none; /* 对于 Chrome, Safari 和 Opera */
	    }

        .cate_list {
            width: 77%;
            margin-top: .36rem;
           
            .cate_tab {
                width: 100%;
                display: flex;
                margin-bottom: .24rem;
                // position: fixed;
                // background-color: #fff;
                div {
                    font-family: PingFangSC, PingFang SC;
                    font-weight: 400;
                    font-size: .24rem;
                    padding: .07rem .32rem;
                    background: #F2F4F5;
                    border-radius: .24rem;
                    color: #666666;
                    border: 1px solid #F2F4F5;
                    margin-right: .32rem;
                }

                .cate_active {
                    background: rgba(255, 98, 0, 0.1);
                    border: 1px solid #FF6200;
                    color: #FF6200;
                }
            }
        }
    }

    .good-ul {
        width: 100%;
        // height: 73vh;
        // padding-bottom: 46px;
        // box-sizing: border-box;
        // overflow: auto;
        .good-ul-li {
            width: 96%;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            padding: .24rem 0;
            border-bottom: 1px solid #F4F4F4;

            img {
                width: 1.2rem;
                height: 1.2rem;
            }

            .good-info {
                width: 76%;

                .good-name {
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                    font-family: PingFangSC, PingFang SC;
                    font-weight: 500;
                    font-size: .24rem;
                    color: #333333;
                    margin-bottom: .32rem;
                }

                .good-price {
                    display: flex;
                    align-items: center;

                    .price {
                        font-family: PingFangSC, PingFang SC;
                        font-weight: 500;
                        font-size: .32rem;
                        color: #FF6200;
                    }

                    .original-price {
                        font-family: PingFangSC, PingFang SC;
                        font-weight: 400;
                        font-size: .2rem;
                        color: #999999;
                        margin-left: .16rem;

                        span {
                            text-decoration: line-through
                        }
                    }
                }
            }

        }
    }
}
</style>